;
(function () {
  new Vue({
    el: '#dd_actual_receipt_report',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      pageCounts: [{
          'name': '10',
          'code': 10
        },
        {
          'name': '15',
          'code': 15
        },
        {
          'name': '20',
          'code': 20
        },
      ],
      currentPage: 1,
      totalPage: 0,
      totalCount: 0,
      limit: ['10'],
			projectIds: [],
      keyword: null,
      rentStatus: [
        {
          'name': '往期',
          'code': 'ODP'
        },
        {
          'name': '本期',
          'code': 'CIP'
        },
        {
          'name': '预收',
          'code': 'PRP'
        },
      ],
      selectedRent: [],
      listData: [],
			loadingFlag: true,
      isExtenseFlag:false,
      expenseList:[],
      selectedExpense:[],

      projectList: [],
      buildingList: [],
      floorList: [],
      unitList: [],

      projectId: [],
      buildingId: [],
      floorId: [],
      unitId: [],
      groupId: null,
      groupOneData: [],
      groupTwoData: [],
      groupThreeData: [],
      selectedGroupData: [],
      beginTime:null,
      endTime:null,
      leftPo:'',
      popoverText:''
    },
    filters: {
			rentState (item) {
				let res;
				// YD预订、IR在租、VA空置、IP即将履约
				switch (item){
					case 'YD':
					res = '预订'
						break;
					case 'IR':
					res = '在租'
						break;
					case 'VA':
					res = '空置'
						break;
					default:
					res ='--'
						break;
				}
				return res
			},
      toZero: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      formatDate: function (time) {
				if (!time) return '--';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum: function (num) {
        return hlNumberUtil.formatNum(num)
      },
    },
    computed: {

    },
    methods: {
      // 获取费项
      getExpenseData() {
        hlhttp.get({
          url: 'expenditures',
          data: {page:1,limit:2000},
          success: (data) => {
            data.expenditures.map(item => { item.code = item.id })
            data.expenditures.unshift({'code':'#','name':'全部费项'})
            this.expenseList = data.expenditures
          }
        })

      },
      //获取项目
      getProjectData() {
        hlhttp.get({
          url: 'user/projects',
          success: (res) => {
            res = hlArrayUtil.removeRepeat(res, 'projectId')
            this.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName',rKey: 'name'}, {key: 'projectId',rKey: 'code'}])

            this.getData()
          }
        })
      },
      // 获取一级组合数据
      getGroupPrimaryData(data) {
        const self = this
        self.projectId = [data[0].code]
        self.groupId = null
        self.buildingId = []
        self.floorId = []
        self.unitId = []
        self.buildingList = []
        self.floorList = []
        self.unitList = []
        self.selectedGroupData = []

        self.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData(data, [{
            key: 'id',
            rKey: 'code'
          }]);
        })

        self.getBuildingData()
      },
      // 选取一级组合数据
      selectOneGroup(res) {
        const self = this
        this.groupId = res['cur']['code']

        this.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData(data, [{
            key: 'id',
            rKey: 'code'
          }])
          self.getBuildingData()
        })
      },
      // 选取二级组合数据
      selectTwoGroup(res) {
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData(data, [{
            key: 'id',
            rKey: 'code'
          }])
          self.getBuildingData()
        })
      },
      // 选取三级组合数据
      selectThreeGroup(res) {
        this.groupId = res['cur']['code']
      },
      getGroupData() {
        const self = this
        return new Promise(resolve => {
          hlhttp.get({
            url: 'project/grouping_info/_subclass',
            data: {
              'project_id': self.projectId[0],
              'grouping_id': self.groupId || '#'
            },
            success: function (data) {
              resolve(data)
            }
          })
        })
      },
      //获取楼宇
      getBuildingData() {
        const self = this
        hlhttp.get({
          url: 'project/buildings',
          data: {
            project_id: self.projectId.length ? self.projectId[0] : '#',
            grouping_id: self.groupId || '#'
          },
          success: (res) => {
            this.buildingList = hlArrayUtil.replaceData( res, [{key: 'buildingId',rKey: 'code'}] )
          }
        })
      },
      //获取楼层
      getFloorData() {
        hlhttp.get({
          url: 'building/floors',
          data: {
            project_id: this.projectId[0],
            building_id: this.buildingId[0]
          },
          success: (res) => {
            this.floorList = hlArrayUtil.replaceData( res, [{key: 'floorName',rKey: 'name'}, {key: 'floorId',rKey: 'code'}] )
          }
        })
      },
      //获取商铺
      getUnitData() {
        hlhttp.get({
          url: 'floor/units',
          data: {
            project_id: this.projectId[0],
            building_id: this.buildingId[0],
            floor_id: this.floorId[0]
          },
          success: (res) => {
            this.unitList = hlArrayUtil.replaceData( res, [{key: 'unitName',rKey: 'name'}, {key: 'unitId',rKey: 'code'}] )
          }
        })
      },
      //点击楼宇获得 id
      getbuildingId() {
        this.floorId = [];
        this.unitId = [];
        this.getFloorData()
      },
      //点击楼层获得 id
      getfloorId() {
        this.unitId = [];
        this.getUnitData()
      },
      handleSelectExpense(data){
        console.log(data);
      },
      openSearch(){
        if(this.isExtenseFlag == true){
          this.isExtenseFlag = false;
          return false;
        }

        if(this.isExtenseFlag == false){
          this.isExtenseFlag = true;
          return false;
        }
      },
			rentClick() {
				this.loadingFlag = true
				this.getData()
			},
      // 获取列表数据
      getData() {
        let data = {
          project_id: this.projectId.length ? this.projectId[0] : '#',
          building_id: this.buildingId.length ? this.buildingId[0] : '#',
          floor_id: this.floorId.length ? this.floorId[0] : '#',
          unit_id: this.unitId.length ? this.unitId[0] : '#',
          expenditure_id: this.selectedExpense.length ? this.selectedExpense[0] : '#',
          customer_name: this.keyword || '#',
          bill_start_day: this.beginTime || '#',
          bill_end_day: this.endTime || '#',
          payment_type: this.selectedRent + '' || '#',
          page: this.currentPage,
          limit: this.limit + '',
        }

        hlhttp.get({
          url: 'report/bill/transaction_daily_map',
          data: data,
          success: res => {
            this.totalPage = res.totalPage
            this.totalCount = res.totalCount
            this.listData = res.datas
						this.loadingFlag = false
          }
        })
      },
      // 查询
      search() {
				this.currentPage = 1
				this.loadingFlag = true
				this.getData()
      },
      // 重置
      reset() {
				this.loadingFlag = true

        this.projectId = []
        this.buildingId = []
        this.floorId = []
        this.unitId = []
        this.keyword = null

        this.selectedExpense = []
        this.selectedRent = []
        this.beginTime = this.getCurrentYearMonth()
        this.endTime = this.lastDayDate(0)

        this.currentPage = 1
        this.getData()
      },
      resetTabbar() {
        this.selectedRent = []
        this.currentPage = 1
        this.getData()
      },
      // 返回当前前n天的日期
      lastDayDate(count) {
        var dd = new Date();
        dd.setDate(dd.getDate() + count); //获取count天后的日期
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;//获取当前月份的日期
        var d = dd.getDate();

        function tzero(n) {
          return n < 10 ? '0' + n : '' + n
        }

        return y + "-" + tzero(m) + "-" + tzero(d);
      },
      getCurrentYearMonth() {
        var y = new Date().getFullYear()

        function tzero(n) {
          return n < 10 ? '0' + n : '' + n
        }

        return y + '-01-01'
      },
      // 导出
      excl() {
				hlhttp.get({
				  baseUrl: exclUrl,
				  url: 'report/bill/transaction_daily_map/list_export',
				  data: {
            project_id: this.projectId.length ? this.projectId[0] : '#',
            building_id: this.buildingId.length ? this.buildingId[0] : '#',
            floor_id: this.floorId.length ? this.floorId[0] : '#',
            unit_id: this.unitId.length ? this.unitId[0] : '#',
            expenditure_id: this.selectedExpense.length ? this.selectedExpense[0] : '#',
            customer_name: this.keyword || '#',
            bill_start_day: this.beginTime || '#',
            bill_end_day: this.endTime || '#',
            payment_type: this.selectedRent + '' || '#',
				  },
				  success: (data) => {
				    var a = document.createElement('a');
				    a.href = data.excel_url;
				    $('body').append($(a))
				    a.click();
				    setTimeout(function() {
				      $(a).remove()
				    }, 100);

				  }
				})
      },
      mousemove(e){
        if(e.target.innerText=='往期'){
          this.leftPo = '20%'
          this.popoverText = '当月收到账单截止日在上月或更早的金额'
        }else if(e.target.innerText=='本期'){
          this.leftPo = '40%'
          this.popoverText = '当月收到账单截止日在本月的金额'
        }else{
          this.leftPo = '55%'
          this.popoverText = '当月收到账单截止日在下月之后的金额'
        }
      },
      mouseleave(e){
        this.leftPo = ''
        this.popoverText = ''
      }
    },
    created() {
      this.beginTime = this.getCurrentYearMonth()
      this.endTime = this.lastDayDate(0)
    },
    mounted() {
      this.getProjectData()
      this.getExpenseData()
    }
  })

}())
